//
//  JLCFRefreshButton.m
//  QTZ
//
//  Created by ski on 2017/8/15.
//  Copyright © 2017年 ski. All rights reserved.
//

#import "JLCFRefreshButton.h"

@interface JLCFRefreshButton ()
{
    UIColor *_touchColor;
    UIColor *_normalColor;
}

@property (nonatomic, strong) UIActivityIndicatorView *activityIndicator;

@property (nonatomic, strong) UIButton *refreshBtn;

@property (nonatomic, strong) UIColor *activityIndicatorColor;

//点击事件
@property (nullable, nonatomic, copy) TouchUpInside touchDown;

@end

@implementation JLCFRefreshButton

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.refreshBtn = [[UIButton alloc] initWithFrame:self.bounds];
        self.refreshBtn.titleLabel.font = FONT(14);
        [self addSubview:self.refreshBtn];
    }
    return self;
}

- (void)handleBtn
{
    [self.refreshBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 10)];
    if (!self.activityIndicator) {
        self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
        self.activityIndicator.centerY = self.refreshBtn.height / 2;
        self.activityIndicator.left = self.refreshBtn.titleLabel.right;
        self.activityIndicator.color = self.activityIndicatorColor ? self.activityIndicatorColor : QTZ_White;
    }
    if (!self.activityIndicator.superview) {
        [self.refreshBtn addSubview:self.activityIndicator];
        [self.activityIndicator startAnimating];
    }
    self.refreshBtn.userInteractionEnabled = NO;
    self.touchDown();
}

- (void)handleBtnWithBlock:(TouchUpInside)block
{
    self.touchDown = block;
    [self.refreshBtn addTarget:self action:@selector(handleBtn) forControlEvents:UIControlEventTouchUpInside];
}

- (void)reset
{
    [self.refreshBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    self.refreshBtn.userInteractionEnabled = YES;
    [self.activityIndicator stopAnimating];
    [self.activityIndicator removeFromSuperview];
}

//属性
- (void)setBackgroundColor:(UIColor *)backgroundColor
{
    _normalColor = backgroundColor;
    [self.refreshBtn setBackgroundColor:backgroundColor];
}

- (void)setTouchDownColor:(UIColor *)touchDownColor
{
    _touchColor = touchDownColor;
    [self.refreshBtn addTarget:self action:@selector(changeTouchDownColor) forControlEvents:UIControlEventTouchDown];
    [self.refreshBtn addTarget:self action:@selector(resetColor) forControlEvents:UIControlEventTouchUpInside];
    [self.refreshBtn addTarget:self action:@selector(resetColor) forControlEvents:UIControlEventTouchUpOutside];
}

- (void)changeTouchDownColor
{
    [self.refreshBtn setBackgroundColor:_touchColor];
}

- (void)resetColor
{
    [self.refreshBtn setBackgroundColor:_normalColor];
}

//方法
- (void)setLoadingColor:(UIColor *)color
{
    self.activityIndicatorColor = color;
}

- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state
{
    [self.refreshBtn setTitle:title forState:state];
}

- (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state
{
    [self.refreshBtn setTitleColor:color forState:state];
}

- (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state
{
    [self.refreshBtn setTitleShadowColor:color forState:state];
}

- (void)setImage:(nullable UIImage *)image forState:(UIControlState)state
{
    [self.refreshBtn setImage:image forState:state];
}

- (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state
{
    [self.refreshBtn setBackgroundImage:image forState:state];
}

- (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state
{
    [self.refreshBtn setAttributedTitle:title forState:state];
}

@end
